package com.crape.topic.bo.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.crape.account.bo.IAccountBO;
import com.crape.common.exceptions.BOException;
import com.crape.common.model.Account;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.Article;
import com.crape.common.model.ArticleIndex;
import com.crape.common.model.Subject;
import com.crape.topic.bo.IArticleIndexBO;
import com.crape.topic.bo.ISubjectBO;
import com.crape.topic.dao.IArticleIndexDAO;

public class ArticleIndexBOImpl implements IArticleIndexBO {

	private IArticleIndexDAO articleIndexDAOImpl;
	private ISubjectBO subjectBOImpl;
	private IAccountBO accountBOImpl;

	@Autowired
	public void setarticleIndexDAOImpl(IArticleIndexDAO articleIndexDAOImpl) {
		this.articleIndexDAOImpl = articleIndexDAOImpl;
	}

	@Autowired
	public void setsubjectBOImpl(ISubjectBO subjectBOImpl) {
		this.subjectBOImpl = subjectBOImpl;
	}

	@Autowired
	public void setAccountBOImpl(IAccountBO accountBOImpl) {
		this.accountBOImpl = accountBOImpl;
	}

	@Override
	public void recommendArticle(Long accountid, Long articleid, Integer indexType) throws BOException {
		ArticleIndex articleIndex = articleIndexDAOImpl.getArticleIndexByArticleid(articleid);
		if (articleIndex == null) {
			articleIndex = new ArticleIndex();
			articleIndex.setArticleid(articleid);
			articleIndex.setAccountid(accountid);
			articleIndex.setType(indexType);
			articleIndexDAOImpl.insertArticleIndex(articleIndex);
		} else {
			articleIndex.setArticleid(articleid);
			articleIndex.setAccountid(accountid);
			articleIndex.setType(indexType);
			articleIndexDAOImpl.updateArticleIndex(articleIndex);
		}
	}

	@Override
	public List<Article> getIndexArticles(Long accountid, Long articleid, Integer indexType, Integer status, Integer startIndex, Integer pageSize)
			throws BOException {
		List<Article> resultList = articleIndexDAOImpl.getIndexArticles(accountid, articleid, indexType, status, startIndex, pageSize);
		if (resultList != null && !resultList.isEmpty()) {
			for (Article obj : resultList) {
				// 设置主题title
				Long subjectid = obj.getSubjectid();
				Subject subject = subjectBOImpl.getSubjectById(subjectid);
				obj.setSubjectTitle(subject.getTitle());
				AccountInfo account = accountBOImpl.getAccountInfoById(obj.getAccountid());
				if (account != null) {
					obj.setUsername(account.getUsername());
					obj.setCreator(account);
				}
			}
		}
		return resultList;
	}

	@Override
	public Long countIndexArticles(Long accountid, Long articleid, Integer indexType, Integer status) throws BOException {
		return articleIndexDAOImpl.countIndexArticles(accountid, articleid, indexType, status);
	}

	@Override
	public List<Article> getIndexShowArticles(Integer indexType, Integer showNum) throws BOException {
		List<Article> resultList = articleIndexDAOImpl.getIndexShowArticles(indexType, showNum);
		if (resultList == null) {
			return new ArrayList<Article>(showNum);
		}
		for (Article obj : resultList) {
			// 设置主题title
			Long subjectid = obj.getSubjectid();
			Subject subject = subjectBOImpl.getSubjectById(subjectid);
			obj.setSubjectTitle(subject.getTitle());
			AccountInfo account = accountBOImpl.getAccountInfoById(obj.getAccountid());
			if (account != null) {
				obj.setUsername(account.getUsername());
				obj.setCreator(account);
			}
		}
		return resultList;
	}

}
